package com.yanwq.sayhi.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yanwq.sayhi.MApplication;
import com.yanwq.sayhi.R;

/**
 * 作者：yanwq。
 * 日期：2016/1/30。
 */
public class MToast {

    public static void showShort(String text) {
        showShort(MApplication.getContext(), text);
    }

    public static void showShort(Context context, int resId) {
        show(context, MApplication.getContext().getString(resId), Toast.LENGTH_SHORT);
    }

    public static void showShort(Context context, String text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, final String text, int duration) {
        final Toast toast = new Toast(context);

        @SuppressLint("InflateParams") View view = LayoutInflater.from(context)
                                                                 .inflate(R.layout.toast,
                                                                          null);
        view.setMinimumWidth(context.getResources().getDisplayMetrics().widthPixels);
        ((TextView) view.findViewById(R.id.toast_content)).setText(text);
        view.findViewById(R.id.toast_content).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                toast.cancel();
                return false;
            }
        });

        view.findViewById(R.id.toast_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast.cancel();
            }
        });

        toast.setView(view);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
